﻿namespace HttpProblem
{
    //大件316项目，在进行大批量网络请求之后会请求失败，疑似网络端口占满，
    //但是用资源监视器以及命令行工具查看，并没有全部占满，
    //理论上2分钟之后占用的端口应该也释放，但是http之后就连不上了
    internal class Program
    {
        //private static readonly HttpClient httpClient = new HttpClient();
        //private const int MaxConcurrentRequests = 10; // 最大并发请求数
        //private static SemaphoreSlim semaphore = new SemaphoreSlim(MaxConcurrentRequests);

        //static async Task Main(string[] args)
        //{
        //    var urls = new string[]
        //    {
        //    "https://example.com",
        //    "https://example.org",
        //        // 添加更多的 URL
        //    };

        //    var tasks = new Task[urls.Length];

        //    for (int i = 0; i < urls.Length; i++)
        //    {
        //        tasks[i] = ProcessUrlAsync(urls[i]);
        //    }

        //    await Task.WhenAll(tasks);

        //    Console.ReadKey();
        //}

        //private static async Task ProcessUrlAsync(string url)
        //{
        //    await semaphore.WaitAsync(); // 等待可用的信号量

        //    try
        //    {
        //        var response = await httpClient.GetAsync(url);
        //        response.EnsureSuccessStatusCode(); // 确保响应成功
        //        var content = await response.Content.ReadAsStringAsync();
        //        Console.WriteLine($"Response from {url}: {content.Substring(0, 100)}..."); // 打印前100个字符
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine($"Error fetching {url}: {ex.Message}");
        //    }
        //    finally
        //    {
        //        semaphore.Release(); // 释放信号量
        //    }
        //}


        //private static readonly HttpClient client = new HttpClient();
        //static async Task Main(string[] args)
        //{
        //    int numberOfRequests = 100000; // 模拟的请求数量
        //    Task[] tasks = new Task[numberOfRequests];
        //    for (int i = 0; i < numberOfRequests; i++)
        //    {
        //        tasks[i] = SendRequestAsync(i);
        //    }
        //    await Task.WhenAll(tasks);
        //}
        //private static async Task SendRequestAsync(int requestId)
        //{
        //    try
        //    {
        //        string url = "http://www.baidu.com";
        //        var response = await client.GetAsync(url);
        //        Console.WriteLine($"Request {requestId}: {response.StatusCode}");
        //    }
        //    catch (HttpRequestException e)
        //    {
        //        Console.WriteLine($"Request {requestId} failed: {e.Message}");
        //    }
        //}


        private static readonly HttpClient client = new HttpClient();
        private static readonly SemaphoreSlim semaphore = new SemaphoreSlim(100); // 限制并发请求数量
        static async Task Main(string[] args)
        {
            int totalRequests = 1000; // 总请求数量
            int requestsPerSecond = 500; // 每秒请求数量
            int delayBetweenRequests = 1000 / requestsPerSecond; // 每个请求之间的延迟
            for (int i = 0; i < totalRequests; i++)
            {
                await semaphore.WaitAsync(); // 等待可用的信号量
                _ = SendRequestAsync(i); // 发送请求
                await Task.Delay(delayBetweenRequests); // 控制请求速率
            }
            // 等待所有请求完成
            await Task.Delay(5000); // 等待一段时间以确保所有请求完成
        }
        private static async Task SendRequestAsync(int requestId)
        {
            try
            {
                string url = "http://www.baidu.com";
                var response = await client.GetAsync(url);
                Console.WriteLine($"Request {requestId}: {response.StatusCode}");
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request {requestId} failed: {e.Message}");
            }
            finally
            {
                semaphore.Release(); // 释放信号量
            }
        }
    }
}
